package com.will.client.heart;


import com.will.entity.PingMsg;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author 杨明智
 * @version:1.0
 * @create 2018年10月30日 2:56 PM
 * @description
 **/
@Sharable
public class ConnectorIdleStateTrigger extends ChannelInboundHandlerAdapter {



    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.WRITER_IDLE) {
                // write heartbeat to server
                System.out.println("没有写操作");
                PingMsg pingMsg = new PingMsg();
                pingMsg.setMsg("这是来自客户端的心跳包");
                ctx.writeAndFlush(pingMsg);
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
